package testApplets;

import java.util.Vector;

import meshToSolid.Selector;

import processing.core.PApplet;
import simpleGeom.Bounds3D;
import simpleGeom.Face3D;
import simpleGeom.HasPos3D;
import simpleGeom.Mesh;
import simpleGeom.Node;
import simpleGeom.Plane;
import simpleGeom.Vec3D;


public class FinalSelectApplet extends PApplet {
	String folderPath="/Users/caad/Desktop/Grotto - First Half V/03_scaled/";
	//String inputPath=folderPath+"grottoVolume.obj";
	String inputPath=folderPath+"final_19_1067.obj";
	
	String outputPath=inputPath+"Sel.obj";
	Bounds3D b3d;
	Selector selector;
	Face3D maxX;
public void setup(){
	size(1600,900,P3D);
	selectBiggest();
	//mirror();
	exit();
}
public void mirror(){
	String input=this.selectInput();
	Mesh mesh=new Mesh(input);
	Bounds3D b3d=mesh.getBounds();
	float cZ=b3d.getZ2();
	System.out.println(cZ);
	//
	//mesh.trimMeshZPlane(new Plane(new Vec3D(0,0,0.0002f),new Vec3D(0,0,-1)),0.0002f);
	mesh.translate(0, 0, -cZ);
	mesh.mirrorAndFuse(new Plane(new Vec3D(0,0,0),new Vec3D(0,0,-1)));
	mesh.scale(1000, 1000, 1000);
	mesh.saveMesh(input+"trim.obj");
}
public void selectSpecialGroup(){
	selector=new Selector();
	selector.loadOBJ(inputPath);
	Bounds3D b3d=selector.b3d;
	
	

	/*float off=0.1f;
	b3d.x1+=0.1f;
	b3d.y1+=0.3f;
	b3d.z1+=0.2;
	b3d.x2+=0.04F; //0.05F
	b3d.y2-=0.1F; //-0.06F
	b3d.z2-=0.2f;*/
	
	//b3d.x2+=off*2;
	
	//Face3D maxX=selector.getMaxXFace();
	maxX=selector.getRayFace(b3d.x1, b3d.getCenterY()+0.01f, b3d.getCenterZ()+0.01f, 1, 0, 0);
	
	//b3d.getOffset();
	//Vector<Face3D>selectedFaces=selector.selectFaces(selector.faces, b3d, maxX, true);
	println(selector.faces.size());
	Vector<Face3D>selectedFaces=selector.selectConnectedFaces(selector.faces, b3d, selector.faces.firstElement());
	selector.saveObjPrintwriter(this,outputPath, selectedFaces);
	println(selectedFaces.size());
	println("saved");
}

public void selectBiggest(){
	selector=new Selector();
	inputPath=this.selectInput();
	selector.loadOBJ(inputPath);
	/*b3d=selector.b3d;
	float scale1=200f/b3d.getDimX();
	float scale2=150f/b3d.getDimY();
	float scale=Math.min(scale1, scale2);
	println("scale: "+scale);
	for (int i=0;i<selector.nodes.size();i++){
		Node node=(Node) selector.nodes.get(i);
		node.x*=scale;
		node.y*=scale;
		node.z*=scale;
	}*/
	int[] groupIds=selector.getGroups(selector.faces);
	int nGroups=0;
	for (int i=0;i<groupIds.length;i++){
		if (groupIds[i]>=nGroups){
			nGroups=groupIds[i];
		}
	}
	int[] nFaces=new int[nGroups+1];
	for (int i=0;i<groupIds.length;i++){
		nFaces[groupIds[i]]++;
	}
	int maxGroup=0;
	int maxFaces=0;
	for (int i=0;i<nFaces.length;i++){
		if (nFaces[i]>maxFaces){
			maxFaces=nFaces[i];
			maxGroup=i;
		}
	}
	Vector<Face3D>groupFaces=new 	Vector<Face3D>();
	for (int i=0;i<groupIds.length;i++){
		if (groupIds[i]==maxGroup){
			groupFaces.add(selector.faces.get(i));
		}
	}
	/*for (int i=0;i<nFaces.length;i++){
		if (nFaces[i]>10000){
			groupFaces=new 	Vector<Face3D>();
			for (int ii=0;ii<groupIds.length;ii++){
				if (groupIds[ii]==i){
					groupFaces.add(selector.faces.get(ii));
				}
			}
			selector.saveObjPrintwriter(this, inputPath+"g_"+i+"sel.obj", groupFaces);
		}
	}*/
	selector.saveObjPrintwriter(this, inputPath+"sel.obj", groupFaces);
	
	
}
public void draw(){
	fill(255);
	noStroke();
	lights();
	camera(b3d.getX1()-400,b3d.getCenterY(),b3d.getCenterZ(),b3d.getX2(),b3d.getCenterY(),b3d.getCenterZ(),0,-1,0);
	translate(-b3d.getCenterX(),-b3d.getCenterY(),-b3d.getCenterZ());
	scale(100);
	for (int i=0;i<selector.faces.size();i++){
		Face3D f=selector.faces.get(i);
		f.display3D(g);
	}
	noFill();
	stroke(255,0,0);
	b3d.display3D(g);
	noStroke();
	fill(255,0,0);
	maxX.display3D(g);
}
}
